<template>
	<view>
		<view class="flex flex-column">
			<image :showLoading="true" :src="src" width="80px" height="80px" style="width: 100%;height: 150px;"></image>
			<view class=" padding background" style="margin-top:-40px ;">
				<view class="flex flex-row">
					<u-avatar :src="userInfo.avatarUrl" shape="circle" mode="scaleToFill" size="70"></u-avatar>
					<view style="margin-left: 50px;"></view>
					<u--text :lines="1" :text="userInfo.username"></u--text>
				</view>
				<view class="margin-top"></view>
				<u--text :lines="1" :text="'个性签名: '+userInfo.signature"></u--text>
				<view class="flex flex-row margin-top">
					<u--text :lines="1" :text="'关注    '+attention"></u--text>
					<u--text :lines="1" :text="'粉丝    '+follwer"></u--text>
				</view>
			</view>
			<u-divider textColor="#2979ff" lineColor="#ff0000"></u-divider>

			<view class="flex flex-column">
				<view v-for="(post,index) in posts" :key="index" class="background padding border-radius"
					style="width: 100%;">
					<view class="flex flex-row ">
						<u-avatar :src="userInfo.avatarUrl" shape="circle" mode="scaleToFill" size="50"></u-avatar>
						<view style="margin-left: 30px;"></view>
						<u--text :lines="1" :text="userInfo.username"></u--text>
					</view>
					<view style="margin-top: 15px;"></view>
					<u--text color="black" size="25" :text="post.title"></u--text>
					<view style="margin-top: 5px;"></view>
					<u--text size="20" :lines='2' :text="post.content"></u--text>
					<u--text :text="formatCreatedAt(post.createdAt)"></u--text>
					<!-- 发布的图片 -->
					<view class="image-container">
						<view v-for="(image, index) in post.imageUrls.split(',')" :key="index" class="image-wrapper">
							<image style="height: 100px;width: 95px;"
								:src="`http://localhost:8090/file/user/${post.userId}/posts/${image}`"></image>
						</view>
					</view>
					<u-divider textColor="#2979ff" lineColor="#ff0000"></u-divider>

				</view>

			</view>


		</view>
	</view>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				src: 'https://images.pexels.com/photos/869258/pexels-photo-869258.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1',
				userId: null,
				userInfo: [],
				attention: 0,
				follwer: 0,
				posts: [],
				albumWidth: 0,
				urls2: [
					"https://xxx.com/album/1.jpg",
					"https://xxx.com/album/2.jpg",
					"https://xxx.com/album/3.jpg",
					"https://xxx.com/album/4.jpg",
					"https://xxx.com/album/5.jpg",
					"https://xxx.com/album/6.jpg",
					"https://xxx.com/album/7.jpg",
					"https://xxx.com/album/8.jpg",
					"https://xxx.com/album/9.jpg",
					"https://xxx.com/album/10.jpg",
				],


			}
		},
		onLoad(options) {
			this.userId = options.userId;
			console.log(this.userId);
			this.getUserInfo()
			this.fetchPosts()
		},
		methods: {
			getUserInfo() {
				// 从服务器获取用户发布的帖子
				uni.request({
					url: `http://localhost:8090/user/${this.userId}`,
					method: 'GET',
					success: (res) => {
						if (res.data.code == 200) {
							this.userInfo = res.data.data;
							// console.log('用户信息', res.data.data)
						} else {
							uni.showToast({
								title: '获取帖子失败',
								duration: 2000,
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '请求失败',
							duration: 2000,
							icon: 'none'
						});
					}
				});
			},

			fetchPosts() {
				// 从服务器获取用户发布的帖子
				uni.request({
					url: `http://localhost:8090/post/user/${this.userId}`,
					method: 'GET',
					success: (res) => {
						if (res.data.code == 200) {
							this.posts = res.data.data;
							console.log('用户发布的帖子', res.data.data)

						} else {
							uni.showToast({
								title: '获取帖子失败',
								duration: 2000,
								icon: 'none'
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '请求失败',
							duration: 2000,
							icon: 'none'
						});
					}
				});
			},

			formatCreatedAt(time) {
				return time ? moment(time).format('YYYY-MM-DD') : '';
			},



		}
	}
</script>

<style>
	.image-container {
		display: flex;
		flex-wrap: wrap;
	}

	.image-wrapper {
		width: 30%;
		padding: 1px;
	}
</style>